﻿
@{
    ViewBag.Title = "FileView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
}
<style>
    /* 子侧边 */
    .layui-side-child {
        width: 160px !important;
        /*left: 200px;*/
        bottom: 60px !important;
        border-right: 1px solid #eee;
        background-color: #fff;
    }

        .layui-side-child .layui-side-scroll {
            width: 170px;
        }

        .layui-side-child .layui-nav {
            padding: 10px 0;
            width: 160px;
            border-radius: 0;
            background: none
        }

        .layui-side-child .layui-nav-child {
            border-radius: 0;
        }

        .layui-side-child .layui-nav .layui-nav-title a,
        .layui-side-child .layui-nav .layui-nav-title a:hover,
        .layui-side-child .layui-nav-itemed > a {
            color: #666 !important;
        }

        .layui-side-child .layui-nav-itemed .layui-nav-child {
            margin-bottom: 10px;
            background: none !important;
        }

        .layui-side-child .layui-nav .layui-nav-item a {
            height: 30px;
            line-height: 30px;
            color: #666;
        }

            .layui-side-child .layui-nav .layui-nav-item a:hover {
                background: none !important;
            }

        .layui-side-child .layui-nav .layui-nav-child a {
            color: #999 !important;
        }

        .layui-side-child .layui-nav .layui-nav-more {
            display: none;
        }

        .layui-side-child .layui-nav-tree .layui-this,
        .layui-side-child .layui-nav-tree .layui-this > a,
        .layui-side-child .layui-nav-tree .layui-nav-child dd.layui-this,
        .layui-side-child .layui-nav-tree .layui-nav-child dd.layui-this a {
            background: none;
            color: #5FB878 !important;
        }

        .layui-side-child .layui-nav .layui-nav-child a:hover {
            color: #009688 !important
        }

        .layui-side-child .layui-nav-bar {
            background-color: #5FB878;
        }
</style>
<div style="margin-left:50px;width:100%;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">主项目选择</label>
            <div class="layui-input-block">
                <select name="ProjectId" lay-verify="required" lay-search lay-filter="ProjectSelFilter" id="TopProjectId">
                    <option value=""></option>
                    @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                    {
                        <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                    }
                </select>
            </div>
        </div>
    </form>
</div>
<div class="layui-side layui-side-child" style="margin-top:50px;">
    <div class="layui-side-scroll">
        <!-- 左侧子菜单 -->
        <ul class="layui-nav layui-nav-tree site-demo-table-nav" style="height:100%;">
            <li class="layui-nav-item layui-this">
                <a href="/Project/FileView">文件管理</a>
            </li>
            <li class="layui-nav-item">
                <a href="#">待加入<span class="layui-badge-dot"></span></a>
            </li>

            <li class="layui-nav-item ">
                <a href="#">待加入</a>
            </li>
            <li class="layui-nav-item ">
                <a href="#">待加入</a>
            </li>

            <hr>

        </ul>
    </div>
</div>
<div id="ViewBody" style="position:absolute;left:160px;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">上传合同</label>
            <label class="layui-form-label" style="width:280px;">（仅限以下格式：xls|xlsx|doc|docx|zip|rar）</label>
            <div class="layui-input-block">
                @*<input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="关键字" class="layui-input" style="width:300px;float:left;">
                    <input type="text" value="" style="display:none" />
                    <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>*@
                <button type="button" class="layui-btn layui-btn-danger" id="UpLoadExcel">
                    <i class="layui-icon">&#xe67c;</i>上传文件
                </button>
                @*<button type="button" style="display:none" id="UpLoadHideBtn"></button>*@

            </div>
        </div>
    </form>
    <table id="demo2" lay-filter="FileTableList"></table>
</div>

<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>

</script>

<script type="text/javascript">

    var ProjectId = "@ViewBag.ProjectId"

    layui.use(['form', 'table', 'upload', 'layer', 'element'], function(){
        var element = layui.element;
        var upload = layui.upload;
        var table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;

        form.on('select(ProjectSelFilter)', function (data)
        {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            ProjectId = data.value;
            if (ProjectId != "") {
                table.reload("demo2", {
                    where: {
                        SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and ProjectId=" + ProjectId
                    }
                });
            }

        });


        $(".layui-nav-item a").click(function (event)
        {
            event.preventDefault();
            var href = $(this).attr("href");

            href = href + "?ProjectId=" + ProjectId;
            location.href = href;
        });

        table.render({
            elem: '#demo2'
            , method: 'post'
            //, height: 315
            , url: '/Project/FileTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and 1=0" }
            , limit: 20
            , limits: [20, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '文件名', width: 250 }
                , {
                    title: "下载", width: 180,
                    templet: function (d)
                    {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Download">下载</a>';
                    }
                }
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d)
                    {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(FileTableList)", function (obj)
        {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'Download') {
                $.down('/Project/Download', 'Path::' + data.Path + "&Name::" + data.Name, 'post');


            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Name + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index)
                    {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Project/FilelDel",
                            async: false,
                            data: { "FileId": data.FileId },
                            success: function (msg)
                            {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo2", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index)
                    {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

        $("#TopProjectId").val(ProjectId);
        form.render('select');
        if (ProjectId != "") {
            table.reload("demo2", {
                where: {
                    SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and ProjectId=" + ProjectId
                }
            });
        }

        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/Project/Upload/' //上传接口
            , accept: 'file'
            , size: 1024000
            , exts: "xls|xlsx|doc|docx|zip|rar"
            , data: {
                "ProjectId": function ()
                {
                    return ProjectId;
                }
            }
            , before: function (obj)
            { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                LayerIndex = layer.open({
                    type: 1,
                    title: '进度条',
                    area: ['700px', '200'],
                    content: $("#LayerProgBar").html(),
                    closeBtn: 0

                });
                //然后需要重新渲染一下上传进度条
                element.init();
            }
            , progress: function (e, percent)
            {
                console.log("进度：" + percent + '%');
                element.progress('progressBar', percent + '%');
            }
            , done: function (msg, index, upload)
            {
                //上传完毕回调
                layer.close(LayerIndex);
                if (msg.result != "OK") {
                    layer.msg(msg.result, { icon: 6, time: 1000 });
                }
                else {
                    layer.msg('上传成功', { icon: 6, time: 1000 });
                    table.reload("demo2", {
                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                    });
                }

            }
            , error: function ()
            {
                //请求异常回调
                alert(msg.result);
            }
        });


    });



</script>
